/**
 * Sample
 * <p/>
 * User: joakimd
 * Date: 2013-02-04
 * Time: 11:21
 */
package org.dru.test {
import flash.media.Sound;
import flash.utils.ByteArray;

public class Sample {
    private static const MAGIC_DELAY: int = 1257;

    private var _length: int;
    private var _left: Vector.<Number>;
    private var _right: Vector.<Number>;

    public function Sample(sound: Sound) {
        const data: ByteArray = new ByteArray();
        const length: int = sound.length * 44.1 - MAGIC_DELAY * 2;
        sound.extract(data, length, MAGIC_DELAY);
        _length = length;
        _left = new Vector.<Number>(_length, true);
        _right = new Vector.<Number>(_length, true);
        data.position = 0;
        for (var index: int = 0; index < _length; index++) {
            _left[index] = data.readFloat();
            _right[index] = data.readFloat();
        }
    }

    public function get length(): int {
        return _length;
    }

    public function copyTo(sofs: int, dleft: Vector.<Number>, dright: Vector.<Number>, dofs: int, length: int): void {
        for (var index:int = 0; index < length; index++) {
            dleft[dofs + index] = _left[sofs + index];
            dright[dofs + index] = _right[sofs + index];
        }
    }

    public function addTo(sofs: int, dleft: Vector.<Number>, dright: Vector.<Number>, dofs: int, length: int): void {
        for (var index:int = 0; index < length; index++) {
            dleft[dofs + index] += _left[sofs + index];
            dright[dofs + index] += _right[sofs + index];
        }
    }
}
}
